package com.aisino.cvasp.application.service;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;

import java.util.Random;


public class CreateTable {

    public static void createTable(Document document, BaseFont baseFont) throws DocumentException {


        Font titlefont = new Font(baseFont, 8, Font.NORMAL);

        PdfPTable table = new PdfPTable(new float[]{80, 80, 80, 80});
        table.setTotalWidth(520);
        table.setPaddingTop(500);
        table.setLockedWidth(true);
        table.setHorizontalAlignment(Element.ALIGN_CENTER);//居中
        table.setSpacingBefore(10f);

        //表头字段
        String title[] = {"其他国籍或居民身份", "其他国籍护照/身份证件\n" + "号码", "其他国籍护照号码", "如【其他国籍身份证件号\n" + "码】和【其他国籍护照号\n" + "码】均无法提供，请说明\n" + "原因"};
        for (int i = 0; i < title.length; i++) {
            PdfPCell heardCell = new PdfPCell();
            heardCell.setVerticalAlignment(Element.ALIGN_TOP);
            heardCell.setHorizontalAlignment(Element.ALIGN_CENTER);
            heardCell.setPhrase(new Phrase(title[i], titlefont));
            heardCell.setMinimumHeight(20);
            table.addCell(heardCell);
        }

        Font tablefont = new Font(baseFont, 8, Font.NORMAL);
        //列表数据
        for (int c = 1; c < 5; c++) {

            PdfPCell setCell1 = createSetCell(getRandomString(), tablefont);
            PdfPCell setCell2 = createSetCell(getRandomString(), tablefont);
            PdfPCell setCell3 = createSetCell(getRandomString(), tablefont);
            PdfPCell setCell4 = createSetCell(getRandomString()+""+getRandomString(), tablefont);
            table.addCell(setCell1);
            table.addCell(setCell2);
            table.addCell(setCell3);
            table.addCell(setCell4);
        }
        document.add(table);
    }

    public static PdfPCell createSetCell(String value, Font font) {
        PdfPCell cell = new PdfPCell();
        cell.setPhrase(new Phrase(value, font));
        cell.setVerticalAlignment(Element.ALIGN_TOP);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        return cell;
    }

    public static String getRandomString( ){
        Random random=new Random();
         Long b = Math.abs(random.nextLong());
         return b.toString();
    }
}
